import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../models/device/channel.dart';
import '../models/device/device.dart';
import '../services/api_service.dart';

part 'device_providers.g.dart';

@riverpod
Future<List<Device>> deviceList(Ref ref) async {
  final apiService = ref.watch(apiServiceProvider);
  final response = await apiService.getList<Device>(
    '/devices',
    (json) => Device.fromMap(json as ApiJsonMap),
  );
  return response.data ?? [];
}

@riverpod
Future<Device> deviceDetail(Ref ref, int deviceId) async {
  final apiService = ref.watch(apiServiceProvider);
  final response = await apiService.get<Device>(
    '/devices/$deviceId',
    (json) => Device.fromDetailJson(json as ApiJsonMap),
  );
  return response.data!;
}

@riverpod
Future<List<Channel>> channelList(Ref ref, int deviceId) async {
  final apiService = ref.watch(apiServiceProvider);
  final response = await apiService.getList<Channel>(
    '/devices/$deviceId/channels',
    (json) => Channel.fromMap(json as ApiJsonMap),
  );
  return response.data ?? [];
}

@riverpod
Future<Channel> channelDetail(Ref ref, int deviceId, int channelId) async {
  final apiService = ref.watch(apiServiceProvider);
  final response = await apiService.get<Channel>(
    '/channels/$channelId',
    (json) => Channel.fromMap(json as ApiJsonMap),
  );
  return response.data!;
}
